//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;

namespace STT.Business
{
    public class BTbHeSoBaoLuu
    {
        public static DataTable Search(ETbHeSoBaoLuu hsbl)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaHSBL", hsbl.MaHSBL);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbHeSoBaoLuu_Search", pr);
            return all;
        }

        public static bool KiemTraDuLieu(ETbHeSoBaoLuu hsbl)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaHSBL", hsbl.MaHSBL);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbHeSoBaoLuu_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbHeSoBaoLuu_SelectAll", null);
            return all;
        }
        //-----------------------------------------------------------//
        public static void Insert(ETbHeSoBaoLuu OTbHeSoBaoLuu)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaHSBL", OTbHeSoBaoLuu.MaHSBL);
            pr[1] = new SqlParameter(@"MoTa", OTbHeSoBaoLuu.MoTa);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbHeSoBaoLuu_Insert", pr);
        }
        //-----------------------------------------------------------//
        public static void Update(ETbHeSoBaoLuu OTbHeSoBaoLuu)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaHSBL", OTbHeSoBaoLuu.MaHSBL);
            pr[1] = new SqlParameter(@"MoTa", OTbHeSoBaoLuu.MoTa);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbHeSoBaoLuu_Update", pr);
        }
        //-----------------------------------------------------------//
        public static void Delete(double MaHSBL)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaHSBL", MaHSBL);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbHeSoBaoLuu_Delete", pr);
        }
        //-----------------------------------------------------------//
        public static ETbHeSoBaoLuu SelectByID(double MaHSBL)
        {
            ETbHeSoBaoLuu OTbHeSoBaoLuu = new ETbHeSoBaoLuu();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaHSBL", MaHSBL);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbHeSoBaoLuu_SelectByID", pr);
            if (idr.Read())
                OTbHeSoBaoLuu = GetOneTbHeSoBaoLuu(idr);
            idr.Close();
            idr.Dispose();
            return OTbHeSoBaoLuu;
        }
        //-----------------------------------------------------------//
        private static ETbHeSoBaoLuu GetOneTbHeSoBaoLuu(IDataReader idr)
        {
            ETbHeSoBaoLuu OTbHeSoBaoLuu = new ETbHeSoBaoLuu();
            if (idr["MaHSBL"] != DBNull.Value)
                OTbHeSoBaoLuu.MaHSBL = (double)idr["MaHSBL"];
            if (idr["MoTa"] != DBNull.Value)
                OTbHeSoBaoLuu.MoTa = (string)idr["MoTa"];
            return OTbHeSoBaoLuu;
        }
        //-----------------------------------------------------------//
        public static bool TestByID(double MaHSBL)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaHSBL", MaHSBL);
            pr[1] = new SqlParameter(@"TestID", SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbHeSoBaoLuu_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
        //-----------------------------------------------------------//
        public static List<ETbHeSoBaoLuu> ListAll()
        {
            List<ETbHeSoBaoLuu> list = new List<ETbHeSoBaoLuu>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbHeSoBaoLuu_SelectAll", null);
            while (idr.Read())
                list.Add(GetOneTbHeSoBaoLuu(idr));
            if (idr.IsClosed == false)
            {
                idr.Close();
                idr.Dispose();
            }
            return list;
        }
    }

}
